package com.truyen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.ehcache.annotations.Cacheable;
import com.truyen.persistence.dao.ChapterTmpTruyenKiemHiepDao;
import com.truyen.persistence.entity.ChapterTmpTruyenKiemHiep;
import com.truyen.persistence.entity.TruyenKiemHiep;
import com.truyen.service.ChapterTmpTruyenKiemHiepService;
import com.truyen.utils.PagingInfo;


@Service
public class ChapterTmpTruyenKiemHiepServiceImpl implements ChapterTmpTruyenKiemHiepService {
	
	@Autowired
	private ChapterTmpTruyenKiemHiepDao chapterTmpTruyenKiemHiepDao;
	
	@Override
	public void save(ChapterTmpTruyenKiemHiep entity) {
		chapterTmpTruyenKiemHiepDao.save(entity);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenKiemHiepByIdCacheDataController")
	public ChapterTmpTruyenKiemHiep findById(Long id) {
		return chapterTmpTruyenKiemHiepDao.findById(id);
	}

	@Override
	public List<ChapterTmpTruyenKiemHiep> getAll() {
		return chapterTmpTruyenKiemHiepDao.findAll();
	}

	@Override
	public ChapterTmpTruyenKiemHiep findByProperty(String column, Object value) {
		List<ChapterTmpTruyenKiemHiep> lst = chapterTmpTruyenKiemHiepDao.findByQuery(column, value);
		return (lst.size() > 0)?lst.get(0):null;
	}

	@Override
	public List<ChapterTmpTruyenKiemHiep> getLstChapterByTruyen(TruyenKiemHiep entity, String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenKiemHiepDao.getLstChapterByTruyen(entity,kwSearch,sort,sortBy,pInfo);
	}

	@Override
	public ChapterTmpTruyenKiemHiep findAliasvTruyen(TruyenKiemHiep entity, String alias) {
		return chapterTmpTruyenKiemHiepDao.findAliasvTruyen(entity, alias);
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenKiemHiepAllCacheDataController")
	public List<ChapterTmpTruyenKiemHiep> getLstAllChapterTmpTruyenKiemHiep(String kwSearch, String sort, String sortBy, PagingInfo pInfo) {
		return chapterTmpTruyenKiemHiepDao.getLstAllChapterTmpTruyenKiemHiep(kwSearch, sort, sortBy, pInfo);
	}

	@Override
	public void updateChapterNumber(Long id, float chapterNumberChange) {
		try{
			ChapterTmpTruyenKiemHiep chapter = findById(id);
			chapter.setChapterNumber(chapterNumberChange);
			chapterTmpTruyenKiemHiepDao.merge(chapter);
		}catch (Exception e) {
			// TODO: handle exception
		}
		
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenKiemHiepCacheTruyenController")
	public List<ChapterTmpTruyenKiemHiep> getByTruyen(TruyenKiemHiep entity) {
		return chapterTmpTruyenKiemHiepDao.getByTruyen(entity);
	}

	@Override
	public ChapterTmpTruyenKiemHiep getChapterByTruyenChapnumber(TruyenKiemHiep entity, float chapterNumber) {
		return chapterTmpTruyenKiemHiepDao.getChapterByTruyenChapnumber(entity,chapterNumber);
	}

	@Override
	@Transactional
	public void delete(long id) {
		try{
			ChapterTmpTruyenKiemHiep chapter = findById(id);
			if(chapter != null){
				chapterTmpTruyenKiemHiepDao.delete(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	@Cacheable(cacheName="chapterTmpTruyenKiemHiepMoiCapNhatCacheTruyenController")
	public List<ChapterTmpTruyenKiemHiep> getListNewChapter(int maxLimit) {
		return chapterTmpTruyenKiemHiepDao.getListNewChapter(maxLimit);
	}
	
	@Override
	public void update(Long id, Float chapterNumber) {
		try{
			ChapterTmpTruyenKiemHiep chapter = findById(id);
			if(chapter != null){
				chapter.setChapterNumber(chapterNumber);
				chapterTmpTruyenKiemHiepDao.merge(chapter);
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public List<ChapterTmpTruyenKiemHiep> getLstByOrderBy(String columnOrderBy, PagingInfo pInfo) {
		return chapterTmpTruyenKiemHiepDao.getLstByOrderBy(columnOrderBy, pInfo);
	}

	@Override
	public List<ChapterTmpTruyenKiemHiep> getByTruyenPaging(TruyenKiemHiep entity, PagingInfo pInfoChapter) {
		return chapterTmpTruyenKiemHiepDao.getByTruyenPaging(entity,pInfoChapter);
	}

	@Override
	public ChapterTmpTruyenKiemHiep findByTruyenChapterNumber(TruyenKiemHiep entity, float chapterNumber) {
		return chapterTmpTruyenKiemHiepDao.findByTruyenChapterNumber(entity,chapterNumber);
	}

	@Override
	public List<Object> getByIdTruyen(Long idTruyen) {
		return chapterTmpTruyenKiemHiepDao.getByIdTruyen(idTruyen);
	}

}
